package com.kws.commercial.business;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.kws.commercial.dao.CommercialEquipmentLedger;
import com.kws.common.core.exception.ServiceException;
import com.kws.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>UploadEquipmentLedgerBusiness</b>
 *
 * @description: UploadEquipmentLedgerBusiness <br>
 * @date: 2024/10/30 13:28 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Component
public class UploadEquipmentLedgerBusiness {
    private static final Logger logger = LoggerFactory.getLogger(UploadEquipmentLedgerBusiness.class);

    public List<CommercialEquipmentLedger> uploadList(MultipartFile file){
        List<CommercialEquipmentLedger> portList = new ArrayList<>();
        try {
            InputStream ins = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(ins);
            List<Map<String, Object>> maps = reader.readAll();
            // 从第三行开始读取
            AtomicInteger i = new AtomicInteger();
            maps.forEach(column -> {
                CommercialEquipmentLedger equipmentLedger = new CommercialEquipmentLedger();
                if (!String.valueOf(column.get("设备编号")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备编号")))) {
                    equipmentLedger.setDeviceNo(String.valueOf(column.get("设备编号")));
                }
                if (!String.valueOf(column.get("设备名称")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备名称")))) {
                    equipmentLedger.setDeviceName(String.valueOf(column.get("设备名称")));
                }
                if (!String.valueOf(column.get("设备类型")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备类型")))) {
                    equipmentLedger.setDeviceType(String.valueOf(column.get("设备类型")));
                }
                if (!String.valueOf(column.get("所属项目")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("所属项目")))) {
                    equipmentLedger.setProjectQuotationId(Long.getLong(String.valueOf(column.get("所属项目"))));
                }
                if (!String.valueOf(column.get("规格型号")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("规格型号")))) {
                    equipmentLedger.setSpecificationAndModel(String.valueOf(column.get("规格型号")));
                }
                if (!String.valueOf(column.get("设备状态")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备状态")))) {
                    equipmentLedger.setDeviceStatus(String.valueOf(column.get("设备状态")));
                }
                if (!String.valueOf(column.get("安装地点")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("安装地点")))) {
                    equipmentLedger.setInstallAddress(String.valueOf(column.get("安装地点")));
                }
                if (!String.valueOf(column.get("负责人")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("负责人")))) {
                    equipmentLedger.setChargePerson(String.valueOf(column.get("负责人")));
                }
                if (!String.valueOf(column.get("联系电话")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("联系电话")))) {
                    equipmentLedger.setContactNumber(String.valueOf(column.get("联系电话")));
                }
                portList.add(equipmentLedger);
            });
        } catch (IOException e) {

            logger.error("上传数据异常,文件解析:{}", e.getMessage());
            throw new ServiceException("上传数据异常");
        } catch (Exception e) {

            logger.error("上传数据异常,程序: {}", e.getMessage());
            throw new ServiceException("上传数据异常");
        }
        return portList;
    }
}
